package cz.data.domain.system.controller;

import cz.data.common.base.BaseController;
import cz.data.common.base.BaseResponse;
import cz.data.common.annotation.DataInner;
import cz.data.common.security.utils.SecurityUtils;
import cz.data.domain.security.rpc.OAuth2ServiceFeign;
import cz.data.domain.system.model.vo.AuthorizeClientVo;
import cz.data.domain.system.model.vo.UserInfo;
import cz.data.domain.system.service.AuthorizeClientService;
import cz.data.domain.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@Api(tags = "系统管理-登录", hidden = true)
@RestController
public class LoginController extends BaseController {

    @Autowired
    private UserService userService;
    @Resource
    OAuth2ServiceFeign oAuth2ServiceFeign;
    @Resource
    AuthorizeClientService authorizeClientService;

    @ApiOperation(value = "系统管理-登录-用户名登录", hidden = true)
    @DataInner(DataInner.Access.authenticated)
    @GetMapping("/login/username/{username}")
    public UserInfo loginByUsername(@PathVariable String username) {
        return userService.getUserByUsername(username);
    }

    @ApiOperation(value = "系统管理-授权-授权客户端", hidden = true)
    @DataInner(DataInner.Access.authenticated)
    @GetMapping("/login/authorize/client/{client}")
    public AuthorizeClientVo loginByAuthorizeClient(@PathVariable String client) {
        return authorizeClientService.getAuthorizeClientByClientKey(client);
    }

    @ApiOperation(value = "系统管理-登录-登出", hidden = true)
    @DeleteMapping("/oauth/logout")
    public BaseResponse logout(@RequestHeader(HttpHeaders.AUTHORIZATION) String tokenValue) {
        Jwt jwt = SecurityUtils.requireSecurityJwt();
        String jti = jwt.getId();
        return oAuth2ServiceFeign.logout(jti, tokenValue);
    }
}
